#include <xp/msw/vpn.h>
#include <xp/bas/stdio.h>
#include <xp/bas/time.h>

int xp_main ()
{
	xp_vpn_t* vpn;
	int i;

	if (xp_createvpnentry (
		XP_TEXT("test connection"), 
		XP_TEXT("202.172.46.1"),
		XP_VPN_SHOW_PROGRESS |
		XP_VPN_REMOTE_DEFAULT_GATEWAY  |
		XP_VPN_REQUIRE_DATA_ENCRYPTION) == -1)
	{
		xp_fprintf (xp_stderr, XP_TEXT("cannot create vpn entry\n"));
		return -1;
	}

	vpn = xp_vpn_open (XP_NULL, XP_TEXT("test connection"));
	if (vpn == XP_NULL) 
	{
		xp_fprintf (xp_stderr, XP_TEXT("cannot open vpn connection\n"));
		xp_removevpnentry (XP_TEXT("test connection"));
		return -1;
	}

	if (xp_vpn_connect (vpn, 
		XP_TEXT("202.172.46.156"), XP_TEXT("chunghh"),
		XP_TEXT("chunghh"), XP_TEXT("")) == -1)
	{
		xp_fprintf (xp_stderr, XP_TEXT("cannot connect\n"));
		xp_vpn_close (vpn);
		xp_removevpnentry (XP_TEXT("test connection"));
		return -1;
	}

	while (1)
	{
		int status;
		if (xp_vpn_getstatus(vpn, &status) == -1) break;
		xp_printf (XP_TEXT("status = %d\n"), status);

		if (status == XP_VPN_CONNECTED) 
		{
			xp_printf (XP_TEXT("connected....\n"));
			break;
		}

		xp_sleep (1000);
	}

	while (1)
	{
		int status;
		if (xp_vpn_getstatus(vpn, &status) == -1) 
		{
			xp_printf (XP_TEXT("cannot get status....\n"));
			break;
		}

		if (status == XP_VPN_DISCONNECTED) 
		{
			xp_printf (XP_TEXT("disconnected....\n"));
			break;
		}

		xp_sleep (1000);
	}

	xp_vpn_close (vpn);

	xp_removevpnentry (XP_TEXT("test connection"));
	return 0;
}

